import {getLocalStorageItem} from "@/utils/storage-helper";
import Format from '@/utils/format'
import fa from "element-ui/src/locale/lang/fa";
export default {

    data() {
        return {
            // 使用自定义方法将日期格式化为字符串
            timerArray: [new Date()- 3600 * 1000 * 24 * 7, new Date()- 3600 * 1000 * 24 * 1], // 初始化为 null 或合适的日期值，
            sumWaySelectList: [{value: '合计'}, {value: '分日'}, {value: '分周'}, {value: '分月'}],
            promotionDeviceSelcetList: [{value: '全部'}, {value: '移动'}, {value: '计算机'}],
            clickRegionSelcetList: [{value: '整个广告'}, {value: '仅限组件'}],
            checked: false,
            collectionTime: null,
            // 分页参数
            userInfo: getLocalStorageItem('userInfo'),
            tableList:[],
            isShowChart:true,
            tableListTop5:[],
            form: {

                userId: getLocalStorageItem('userInfo').userId,
                pageNum: 1,
                pageSize: 20,
                total: 0,
            },
            equipId: '',
            isAutoRecord: '',
            isShowDialog: false,
            // 是否折叠
            isToggleStatus: false,
            // 采集数据
            collectionList: [],
            pickerType: 'date', // 初始为单日期选择
            // 时间选择器配置
            pickerOptions: {
                shortcuts: [
                    {
                        text: '今天',
                        onClick(picker) {
                            const end = new Date()
                            const start = new Date()
                            picker.$emit('pick', [start, end])
                        }
                    },
                    {
                        text: '昨天',
                        onClick(picker) {
                            const end = new Date()
                            const start = new Date()
                            end.setTime(start.getTime() - 3600 * 1000 * 24 * 1)
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 1)
                            picker.$emit('pick', [start, end])
                        }
                    },

                    {
                        text: '最近7天',
                        onClick(picker) {
                            const end = new Date()
                            const start = new Date()
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
                            picker.$emit('pick', [start, end])
                        }
                    },
                    {
                        text: '最近14天',
                        onClick(picker) {
                            const end = new Date()
                            const start = new Date()
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 14)
                            picker.$emit('pick', [start, end])
                        }
                    },
                    {
                        text: '最近30天',
                        onClick(picker) {
                            const end = new Date()
                            const start = new Date()
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
                            picker.$emit('pick', [start, end])
                        }
                    },
                    {
                        text: '上个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date(end.getFullYear(), end.getMonth() - 1, 1);
                            end.setMonth(end.getMonth(), 0);
                            picker.$emit('pick', [start, end]);
                        },
                    },
                    {
                        text: '本月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date(end.getFullYear(), end.getMonth(), 1);
                            picker.$emit('pick', [start, end]);
                        },
                    },
                    {
                        text: '本周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date(end);
                            start.setDate(start.getDate() - start.getDay());
                            picker.$emit('pick', [start, end]);
                        },
                    },
                    {
                        text: '上周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date(end);
                            start.setDate(start.getDate() - start.getDay() - 7);
                            end.setDate(end.getDate() - end.getDay() - 1);
                            picker.$emit('pick', [start, end]);
                        },
                    },
                    {
                        text: '本季度',
                        onClick(picker) {
                            const end = new Date();
                            const quarterStartMonth = Math.floor(end.getMonth() / 3) * 3;
                            const start = new Date(end.getFullYear(), quarterStartMonth, 1);
                            picker.$emit('pick', [start, end]);
                        },
                    },
                    {
                        text: '上季度',
                        onClick(picker) {
                            const end = new Date();
                            const quarterStartMonth = Math.floor(end.getMonth() / 3) * 3;
                            const start = new Date(end.getFullYear(), quarterStartMonth - 3, 1);
                            end.setFullYear(start.getFullYear(), quarterStartMonth, 0);
                            picker.$emit('pick', [start, end]);
                        },
                    },
                    {
                        text: '本年',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date(end.getFullYear(), 0, 1);
                            picker.$emit('pick', [start, end]);
                        },
                    },
                    {
                        text: '去年',
                        onClick(picker) {
                            const end = new Date(new Date().getFullYear(), 0, 0);
                            const start = new Date(end.getFullYear(), 0, 1);
                            picker.$emit('pick', [start, end]);
                        },
                    },],
                onPick: obj => {
                    this.picerMinDate = new Date(obj.minDate).getTime()
                },
                disabledDate: time => {
                    // if (this.picerMinDate) {
                    //   // 最多可选3个月
                    //   const LIMIT = 90 * 24 * 60 * 60 * 1000
                    //   const maxTime = this.picerMinDate + LIMIT
                    //   const minTime = this.picerMinDate - LIMIT
                    //   return time.getTime() > maxTime || time.getTime() < minTime
                    // }
                }
            },
            // 列表勾选的数据
            selectionList: [],
            // 展示修改 数据说明弹窗
            isShowRemarkDialog: false
        }
    },

    filters: {
        // 时间格式化
        formatTimer(timer) {
            return Format(new Date(timer), 'yyyy-mm-dd')
        },

    },
    watch: {

    },
    computed: {


    },
    methods: {

        replaceNullWithDash(arr) {
            if (!Array.isArray(arr) || arr.length === 0) {
                return [];  // 如果 arr 不是数组或数组为空，则返回一个空数组，或者根据你的需求进行处理
            }

            // 获取数组的最后一个对象
            const lastObject = arr[arr.length - 1];

            // 遍历对象的属性
            for (let key in lastObject) {
                if (lastObject.hasOwnProperty(key) && lastObject[key] === null) {
                    // 将值为null的属性修改为"-"
                    lastObject[key] = "-";
                }
            }

            return arr;
        },
        handleTabClick(tab) {

            // 刷新页面
            window.location.reload();
        },
        btnXiu() {
            this.$message('服务器繁忙,请稍后重试！');
            return
        },
        formatDate(date) {
            if (!(date instanceof Date)) {
                date = new Date(date);
            }
            let month = '' + (date.getMonth() + 1),
                day = '' + date.getDate(),
                year = date.getFullYear();

            if (month.length < 2) month = '0' + month;
            if (day.length < 2) day = '0' + day;

            return [year, month, day].join('-');
        },
        // 获取当前的序号
        getIndex(index) {
            let param = (this.form.page - 1) * this.form.pageSize + index + 1
            return param
        },
        // 当前页码改变
        handleCurrentChange(val) {
            this.form.pageNum = val
            this.queryList()
        },
        // 分页大小改变
        handleSizeChange(val) {
            this.form.pageSize = val
            this.queryList()
        },
        handleSelectionChange(val) {
            this.selectionList = val
        },

    },
}
